#version 130

uniform vec3 lightDif;
uniform vec3 lightSpc;
uniform vec3 lightAmb;
uniform vec3 materialDif;
uniform vec3 materialSpc;
uniform vec3 materialAmb;
uniform vec3 materialEmi;
uniform float materialShiny;


in vec3 fNormal;
in vec3 fLightDir;
in vec3 fEyeVec;

out vec4 fOut;

void main (void)
{
	vec4 final_color = vec4(1,1,1,1);
	
//The Orange Book states that gl_FrontLightModelProduct.sceneColor is equivalent to:
//gl_FrontMaterial.emission + gl_FrontMaterial.ambient * gl_LightModel.ambient

	vec4 sceneCol = materialEmi + materialAmb * lightAmb;
	vec4 final_color = 
	(sceneCol * gmaterialAmb) + 
	(lightAmb * materialAmb);
							
	vec3 N = normalize(fNormal);
	vec3 L = normalize(fLightDir);
	
	float lambertTerm = dot(N,L);
	
	if(lambertTerm > 0.0)
	{
		final_color += lightDif * 
		               materialDif * 
					   lambertTerm;	
					  
		
		vec3 E = normalize(fEyeVec);
		vec3 R = reflect(-L, N);
		float specular = pow( max(dot(R, E), 0.0), 
		                 materialShiny );
		final_color += lightSpc * 
		               materialSpc * 
					   specular;	
	}

	fOut = final_color;			
}